﻿//-------------------------------------------------------------------------------
// <copyright file="FBSettings.cs" company="TaskyMedia LLC">
//     Copyright (c) TaskyMedia LLC.
// </copyright>
//-------------------------------------------------------------------------------

/*
 * Copyright (c) 2010, TaskyMedia LLC
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 *   Redistributions of source code must retain the above copyright notice, this 
 *     list of conditions and the following disclaimer.
 *   Redistributions in binary form must reproduce the above copyright notice, this
 *     list of conditions and the following disclaimer in the documentation and/or
 *     other materials provided with the distribution.
 *   Neither the name of TaskyMedia LLC nor the names of its contributors may be used
 *   to endorse or promote products derived from this software without specific prior
 *   written permission.
 *   
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE. 
 */

namespace FBGraphApi
{
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// Stores and retrieves the Facebook Application settings from the configuration file
    /// and allows the developer to manually override settings within the code.
    /// </summary>
    public static class FBSettings
    {
        /// <summary>
        /// The Facebook Application Id.
        /// </summary>
        private static string _clientId;

        /// <summary>
        /// The Facebook Application client secret key.
        /// </summary>
        private static string _clientSecret;

        /// <summary>
        /// The requested extended permissions for the Application.
        /// </summary>
        private static List<ExtendedPermission> _requestedPermissions = new List<ExtendedPermission>();

        /// <summary>
        /// Gets or sets the Client Id (Application Id).
        /// </summary>
        /// <value>The Client Id.</value>
        public static string ClientId
        {
            get
            {
                if (String.IsNullOrEmpty(_clientId))
                {
                    try
                    {
                        _clientId = Configuration.ConfigurationSectionHandler.Default.ClientId;
                    }
                    catch
                    {
                        _clientId = String.Empty;
                    }
                }

                return _clientId;
            }

            set
            {
                _clientId = value;
            }
        }

        /// <summary>
        /// Gets or sets the Client Secret (Application Secret).
        /// </summary>
        /// <value>The Client Secret.</value>
        public static string ClientSecret
        {
            get
            {
                if (String.IsNullOrEmpty(_clientSecret))
                {
                    _clientSecret = Configuration.ConfigurationSectionHandler.Default.ClientSecret;
                }

                return _clientSecret;
            }

            set
            {
                _clientSecret = value;
            }
        }

        /// <summary>
        /// Gets the requested extended permissions for the application.
        /// </summary>
        /// <value>An array containing the requested extended permissions.</value>
        public static List<ExtendedPermission> RequestedPermissions
        {
            get
            {
                if (_requestedPermissions.Count == 0)
                {
                    if (Configuration.ConfigurationSectionHandler.Default != null)
                    {
                        int permissionCount = Configuration.ConfigurationSectionHandler.Default.RequestedPermissions.Count;
                        
                        for (int i = 0; i < permissionCount; i++)
                        {
                            _requestedPermissions.Add((ExtendedPermission)Enum.Parse(typeof(ExtendedPermission), Configuration.ConfigurationSectionHandler.Default.RequestedPermissions[i].Name));
                        }
                    }
                }

                return _requestedPermissions;
            }
        }
    }
}
